package com.thwj.project.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thwj.common.annotation.Log;
import com.thwj.common.core.controller.BaseController;
import com.thwj.common.core.domain.AjaxResult;
import com.thwj.common.core.page.TableDataInfo;
import com.thwj.common.enums.BusinessType;
import com.thwj.common.utils.StringUtils;
import com.thwj.common.utils.poi.ExcelUtil;
import com.thwj.project.biz.controller.vo.MerchantApplyVO;
import com.thwj.project.biz.domain.MerchantApply;
import com.thwj.project.biz.enums.MerchantTagEnum;
import com.thwj.project.biz.service.IMerchantApplyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 商家Controller
 *
 * @author generate
 * @date 2023-07-13
 */
@RestController
@RequestMapping("/biz/apply")
public class MerchantApplyController extends BaseController {
    @Autowired
    private IMerchantApplyService merchantApplyService;

/**
 * 查询商家列表
 */
@PreAuthorize("@ss.hasPermi('biz:apply:list')")
@GetMapping("/list")
    public TableDataInfo list(MerchantApply merchantApply) {
        startPage();
        List<MerchantApply> list = merchantApplyService.list(new QueryWrapper<>(merchantApply).orderByDesc("id"));
        return getDataTable(list);
    }

    /**
     * 导出商家列表
     */
    @PreAuthorize("@ss.hasPermi('biz:apply:export')")
    @Log(title = "商家", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MerchantApply merchantApply) {
        List<MerchantApply> list = merchantApplyService.list(new QueryWrapper<>(merchantApply));
        ExcelUtil<MerchantApply> util = new ExcelUtil<MerchantApply>(MerchantApply. class);
        util.exportExcel(response, list, "商家数据");
    }

    /**
     * 获取商家详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:apply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {

        MerchantApply merchantApply = merchantApplyService.getById(id);

        List<String> tagNameList = new ArrayList<>();
        // 读取标签
        if (!StringUtils.isEmpty(merchantApply.getTagIdList())) {
            String tagIdStr[] = merchantApply.getTagIdList().split(",");
            for (int i = 0; i < tagIdStr.length; i++) {
                MerchantTagEnum merchantTagEnum = MerchantTagEnum.getMerchantTagEnumByValue(Integer.valueOf(tagIdStr[i]));
                if(merchantTagEnum!= null) {
                    tagNameList.add(merchantTagEnum.getName());
                }
            }
        }
        MerchantApplyVO merchantApplyVO = new MerchantApplyVO();
        BeanUtils.copyProperties(merchantApply, merchantApplyVO);
        merchantApplyVO.setTagNameList(String.join(",", tagNameList));

        return AjaxResult.success(merchantApplyVO);
    }

    /**
     * 新增商家
     */
    @PreAuthorize("@ss.hasPermi('biz:apply:add')")
    @Log(title = "商家", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MerchantApply merchantApply) {
        return toAjax(merchantApplyService.save(merchantApply));
    }

    /**
     * 修改商家
     */
    @PreAuthorize("@ss.hasPermi('biz:apply:edit')")
    @Log(title = "商家", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MerchantApply merchantApply) {
        return toAjax(merchantApplyService.updateById(merchantApply));
    }



    /**
     * 审核申请
     */
    @PreAuthorize("@ss.hasPermi('biz:apply:edit')")
    @Log(title = "商家", businessType = BusinessType.UPDATE)
    @PostMapping("auditMerchantApply")
    public AjaxResult auditMerchantApply(@RequestBody MerchantApply merchantApply) {
        return toAjax(merchantApplyService.auditMerchantApply(merchantApply));
    }


    /**
     * 删除商家
     */
    @PreAuthorize("@ss.hasPermi('biz:apply:remove')")
    @Log(title = "商家", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(merchantApplyService.removeByIds(Arrays.asList(ids)));
    }
}
